package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.User;
import com.vehicle.exception.VehicleException;
import com.vehicle.feign.ContactFeign;
import com.vehicle.feign.MenuFeign;
import com.vehicle.feignForm.ContactForm;
import com.vehicle.form.UpdateUserForm;
import com.vehicle.mapper.UserMapper;
import com.vehicle.result.ResponseEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.service.UserService;
import com.vehicle.util.MD5Util;
import com.vehicle.util.RegexValidateUtil;
import com.vehicle.vo.UserListVO;
import com.vehicle.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ContactFeign contactFeign;

    @Autowired
    MenuFeign menuFeign;

    @Override
    public void getUserNotExistByTel(String tel) {
        //验证手机号号
        boolean b1 = RegexValidateUtil.checkMobile(tel);
        if(!b1){
            throw new VehicleException(ResponseEnum.MOBILE_ERROR.getMsg());
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
        userQueryWrapper.eq("tel",tel);
        User user = userMapper.selectOne(userQueryWrapper);
        if(user!=null){
            throw new VehicleException(ResponseEnum.MOBILE_EXIST.getMsg());
        }
    }

    @Override
    public User getUserExistByTel(String tel) {
        //验证手机号
//        boolean b1 = RegexValidateUtil.checkMobile(tel);
//        if(!b1){
//            throw new VehicleException(ResponseEnum.MOBILE_ERROR.getMsg());
//        }
        System.out.println("加密后密码为:");
        System.out.println(MD5Util.getSaltMD5("zhang520520"));
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
        userQueryWrapper.eq("tel",tel);
        User user = userMapper.selectOne(userQueryWrapper);
        if(user == null){
            throw new VehicleException(ResponseEnum.MOBILE_IS_NULL.getMsg());
        }
        return user;
    }

    @Override
    public boolean addUserByTelAndPassword(String tel, String password) {
        User user = new User();
        user.setTel(tel);
        user.setPassword(MD5Util.getSaltMD5(password));
        int b = userMapper.insert(user);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public UserLoginVO updateToken(User user, String token) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getUserId());
        updateWrapper.set("token",token);
        int b = userMapper.update(null,updateWrapper);
        if(b==1){
            UserLoginVO userVO = new UserLoginVO();
            BeanUtils.copyProperties(user,userVO);
            userVO.setToken(token);
            int cdId = user.getCdId();
            userVO.setDetail(contactFeign.getDetailById(cdId));
            userVO.setCdId(cdId);
            int collegeId = contactFeign.getCollageIdById(cdId);
            userVO.setCollegeId(collegeId);
            userVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
            int roleId = menuFeign.getRoleIdByUser(user.getUserId());
            userVO.setRoleId(roleId);
            userVO.setRoleName(menuFeign.getRoleNameById(roleId));
            int uniId = contactFeign.getUniverIdById(collegeId);
            userVO.setUniverId(uniId);
            userVO.setUniverName(contactFeign.getNameUniversity(uniId));
            ContactForm contactForm = contactFeign.getContactMessage(user.getContactId());
            BeanUtils.copyProperties(contactForm,userVO);
            System.out.println("在这里完成了吗？：");
            System.out.println(userVO);
            return userVO;
        }
        return null;
    }

    @Override
    public boolean updateUserBaseMessage(UpdateUserForm userForm) {

//        int collegeId = contactFeign.getCollegeIdByName(userForm.getCollegeName());
//        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
//        userUpdateWrapper.eq("user_id",userForm.getUserId());
//        userUpdateWrapper.set("tel",userForm.getTel());
//        userUpdateWrapper.set("college_id",collegeId);
//        int b = userMapper.update(null,userUpdateWrapper);
//        if(b==1){
//            return true;
//        }
        return false;
    }

    @Override
    public Boolean createContactByToken(String token, ContactForm contactForm) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("token",token);
       User user =  userMapper.selectOne(userQueryWrapper);
       Integer in = contactFeign.addContact(contactForm);
       UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
       updateWrapper.eq("user_id",user.getUserId());
       updateWrapper.set("contact_id",in);
       Integer r = userMapper.update(null,updateWrapper);
       if(r==1){
           return true;
       }
        return false;
    }

    @Override
    public Boolean updateContactByToken(ContactForm contactForm) {
        return contactFeign.updateContact(contactForm);
    }

    @Override
    public Boolean deleteContactByToken(String token) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("token",token);
        User user =  userMapper.selectOne(userQueryWrapper);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("token",token);
        updateWrapper.set("contact_id",null);
        contactFeign.deleteContact(user.getContactId());
        return true;
    }

    @Override
    public UserListVO getAllUserList(Integer page, Integer size) {
        Page<User> infoPage = new Page<>(page,size);
        Page<User> userInfoPage = userMapper.selectPage(infoPage,null);
        UserListVO userListVO = new UserListVO();
        userListVO.setSize(userInfoPage.getSize());
        userListVO.setTotal(userInfoPage.getTotal());
        List<User> users = userInfoPage.getRecords();
        List<UserLoginVO> userLoginVOS = new ArrayList<>();
        for (User user:users) {
            UserLoginVO userVO = new UserLoginVO();
            BeanUtils.copyProperties(user,userVO);
            int cdId = user.getCdId();
            userVO.setDetail(contactFeign.getDetailById(cdId));
            userVO.setCdId(cdId);
            int collegeId = contactFeign.getCollageIdById(cdId);
            userVO.setCollegeId(collegeId);
            userVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
            int roleId = menuFeign.getRoleIdByUser(userVO.getUserId());
            userVO.setRoleId(roleId);
            userVO.setRoleName(menuFeign.getRoleNameById(roleId));
            int uniId = contactFeign.getUniverIdById(collegeId);
            userVO.setUniverId(uniId);
            userVO.setUniverName(contactFeign.getNameUniversity(uniId));
            ContactForm contactForm = contactFeign.getContactMessage(user.getContactId());
            BeanUtils.copyProperties(contactForm,userVO);
            System.out.println("在这里完成了吗？：");
            System.out.println(userVO);
            userLoginVOS.add(userVO);
        }
        userListVO.setContent(userLoginVOS);
        return userListVO;
    }

    @Override
    public boolean deleteUserById(Integer id) {
        int b = userMapper.deleteById(id);
        if(b==1){
            return true;
        }
        return false;
    }

    @Override
    public UserLoginVO addOnlyOneUser(User user) {
        int b = userMapper.insert(user);
        QueryWrapper<User> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("user_id",user.getUserId());
        User user1 = userMapper.selectOne(adminQueryWrapper);
        UserLoginVO userVO = new UserLoginVO();
        BeanUtils.copyProperties(user1,userVO);
        return userVO;
    }

    @Override
    public String getTelById(Integer id) {
       User user =  userMapper.selectById(id);
       return user.getTel();
    }

    @Override
    public String getUserNameById(Integer id) {
        User user =  userMapper.selectById(id);
        return user.getUserName();
    }

    @Override
    public List<UserLoginVO> getListByContent(String content) {
        System.out.println("进来了");
        QueryWrapper<User> userQueryWrapper1 = new QueryWrapper<>();
        userQueryWrapper1.like("user_name",content);
        List<User> userList1 = userMapper.selectList(userQueryWrapper1);
        QueryWrapper<User> userQueryWrapper2 = new QueryWrapper<>();
        userQueryWrapper2.like("tel",content);
        List<User> userList2 = userMapper.selectList(userQueryWrapper2);
        for (User u: userList2) {
            if(!userList1.contains(u)){
                userList1.add(u);
            }
        }
        System.out.println("userList为：");
        System.out.println(userList1);
        List<UserLoginVO> userLoginVOS = new ArrayList<>();
        for (User user:userList1) {
            UserLoginVO userVO = new UserLoginVO();
            BeanUtils.copyProperties(user,userVO);
            int cdId = user.getCdId();
            userVO.setDetail(contactFeign.getDetailById(cdId));
            userVO.setCdId(cdId);
            int collegeId = contactFeign.getCollageIdById(cdId);
            userVO.setCollegeId(collegeId);
            userVO.setCollegeName(contactFeign.getCollageNameById(collegeId));
            int roleId = menuFeign.getRoleIdByUser(user.getUserId());
            userVO.setRoleId(roleId);
            userVO.setRoleName(menuFeign.getRoleNameById(roleId));
            int uniId = contactFeign.getUniverIdById(collegeId);
            userVO.setUniverId(uniId);
            userVO.setUniverName(contactFeign.getNameUniversity(uniId));
            ContactForm contactForm = contactFeign.getContactMessage(user.getContactId());
            BeanUtils.copyProperties(contactForm,userVO);
            System.out.println("在这里完成了吗？：");
            System.out.println(userVO);
            userLoginVOS.add(userVO);
        }
        return userLoginVOS;
    }

}
